/**
 * Created by wushanxin on 14-5-22.
 */

/**
 * 模块依赖
 */
var db = require("../lib/db");

//查找用户记录

exports.selectByID = function(roleid, handler) {
    db.execQuery({
        "sql": "SELECT * FROM roles WHERE  id = ?",
        "args": [roleid],
        "handler": handler
    });
};
exports.deleteByID = function(roleid, handler) {
    db.execQuery({
        "sql": "delete FROM roles WHERE  id = ?",
        "args": [roleid],
        "handler": handler
    });
};

exports.insert = function(role, handler) {
    db.execQuery({
        "sql": "insert into roles " +
            " (rolename,usernumber,wifinumber,authnumber,apnumber,adnumber,statnumber,descs,fee,flag,validmonth,childroleid) " +
            " values (?,?,?,?,?,?,?,?,?,?,?,?)",
        "args": [
        role.rolename,
        role.usernumber,
        role.wifinumber,
        role.authnumber,
        role.apnumber,
        role.adnumber,
        role.statnumber,
        role.descs,
        role.fee,
        role.flag,
        role.validmonth,
        role.childroleid],
        "handler": handler
    });
};

exports.update = function(role, handler) {
    db.execQuery({
        "sql": "update roles " +
            " set rolename=?,usernumber=?,wifinumber=?,authnumber=?," +
            "apnumber=?,adnumber=?,statnumber=?,descs=?,fee=?,flag=?,validmonth=?,childroleid=? " +
            " where id =?",
        "args": [
            role.rolename,
            role.usernumber,
            role.wifinumber,
            role.authnumber,
            role.apnumber,
            role.adnumber,
            role.statnumber,
            role.descs,
            role.fee,
            role.flag,
            role.validmonth,
            role.childroleid,
            role.id],
        "handler": handler
    });
};

exports.selectAll = function( handler) {
    db.execQuery({
        "sql": "SELECT * FROM roles order by flag",
        "handler": handler
    });
};

exports.selectForUserinfo = function(id, handler) {
    db.execQuery({
        "sql": "SELECT * FROM roles where id in (?) order by flag desc",
        "args": [id],
        "handler": handler
    });
};


exports.selectChildrolelist = function( childroleid, handler) {
    db.execQuery({
        "sql": "SELECT * FROM roles where id in (?)",
        "args": [childroleid],
        "handler": handler
    });
};